package tech.tongyu.bob.demo.jpa.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.tongyu.bob.demo.jpa.entity.User;
import tech.tongyu.bob.demo.jpa.service.UserRepository;
import java.util.List;
import java.util.Optional;

@Service
public class UserService  {
    private final  UserRepository userRepository;
    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }



    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public Optional<User> getUserById(Long id) {
        return userRepository.findById(id);
    }

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }

    public User getUserByName(String name) {
        return userRepository.findByName(name);
    }

    public User add(User user){
        return userRepository.save(user);
    }
}
